home *** CD-ROM | disk | FTP | other *** search
/ AI Game Programming Wisdom / AIGameProgrammingWisdom.iso / SourceCode / 07 DecisionMaking Architectures / 01 Isla, Blumberg / BBWar.java < prev    next >
Encoding:
Java Source  |  2001-09-24  |  5.4 KB  |  186 lines

  1. package bb;
  2.  
  3. import java.awt.*;
  4. import java.awt.geom.*;
  5. import java.awt.event.*;
  6. import java.util.*;
  7. import javax.swing.*;
  8.  
  9. /*
  10. This is the main BBWar application class, containg the "main" method.
  11. If you want to play around with gross system parameters -- modifying the number
  12. of cities per team, or the number or size of the resource pools, this is 
  13. where to do it.
  14.  
  15. @author naimad
  16. */
  17.  
  18. public class BBWar extends JPanel {
  19.  
  20.     /*
  21.     First, some annoying mouse nonsense.
  22.     */
  23.  
  24.     class MouseManager extends MouseAdapter implements MouseMotionListener {
  25.         double lastx = 0.0;
  26.         double lasty = 0.0;
  27.         
  28.         public void mousePressed(MouseEvent e) {
  29.             lastx = e.getX();
  30.             lasty = e.getY();
  31.         }
  32.  
  33.         public void mouseMoved(MouseEvent e) {
  34.             //do nothing
  35.         }
  36.         
  37.         public void mouseDragged(MouseEvent e) {
  38.             double dy = e.getY() - lasty;
  39.             
  40.             if ((e.getModifiers() & e.BUTTON1_MASK)!=0) {
  41.                 double dx = e.getX() - lastx;
  42.                 translation.x(translation.x()+dx/scale);
  43.                 translation.y(translation.y()+dy/scale);
  44.             }
  45.             else if ((e.getModifiers() & e.BUTTON3_MASK)!=0) {
  46.                 if (dy >= 0) {
  47.                     scale *= (1.0+(double)dy/50.0);
  48.                 }
  49.                 else {
  50.                     scale /= (1.0+(-dy/50.0));
  51.                 }
  52.             }
  53.             
  54.             lastx = e.getX();
  55.             lasty = e.getY();
  56.         }
  57.     }
  58.  
  59.     int xres;
  60.     int yres;
  61.  
  62.     Vec2 translation = new Vec2(0.0,0.0);
  63.     double scale = 1.0;
  64.     World world;
  65.  
  66.     /*
  67.     The main constructor for the BBWar class.
  68.     */
  69.     public BBWar(int xres, int yres, World world) {
  70.         super();
  71.         this.xres = xres;
  72.         this.yres = yres;
  73.         setSize(xres, yres);
  74.  
  75.         this.world = world;
  76.         
  77.         MouseManager mm = new MouseManager();
  78.         addMouseListener(mm);
  79.         addMouseMotionListener(mm);
  80.     }
  81.  
  82.     double time = 0.0;
  83.     public void update() {
  84.         time += 0.25;
  85.  
  86.         world.update(time);        
  87.                 
  88.         repaint();
  89.  
  90.         //This sleep lets the rendering keep up with the
  91.         //simulation loop. Otherwise we start to drop frames like
  92.         //crazy.
  93.         try { Thread.sleep(20); }
  94.         catch(Exception e) {}
  95.     }
  96.  
  97.     /*
  98.     The central draw routine: this method gets called automatically by the 
  99.     Java2 display update thread. We then go through all drawable objects and
  100.     have them do their thing to the "Graphics2D" object that represents the
  101.     drawable surface.
  102.     */
  103.  
  104.     Shape center = new Ellipse2D.Double(0.0,0.0, 10.0, 10.0);
  105.     AffineTransform transform = new AffineTransform();
  106.     public void paint(Graphics g) {
  107.         Graphics2D g2 = (Graphics2D)g;
  108.  
  109.         g2.clearRect(0,0,getWidth(), getHeight());
  110.         g2.translate(getWidth()/2, getHeight()/2);
  111.         g2.scale(scale, scale);
  112.         g2.translate(translation.x(), translation.y());
  113.         
  114.         world.updateGraphics(time, g2);
  115.     }
  116.  
  117.     public final static void main(String[] s) {
  118.         System.out.println("War!");
  119.         
  120.         JFrame bbwarFrame = new JFrame("BBWar!");
  121.         bbwarFrame.getContentPane().setLayout(new BorderLayout());
  122.  
  123.         int xres = 400;
  124.         int yres = 400;
  125.         
  126.         World world = new World(2);
  127.  
  128.         BBWar theGame = new BBWar(xres, yres, world);
  129.         bbwarFrame.getContentPane().add("Center", theGame);
  130.  
  131.         bbwarFrame.setSize(xres, yres);
  132.         bbwarFrame.setVisible(true);
  133.         
  134.         /*
  135.         This is where we finally set up the two teams. Feel free to muck about with this
  136.         code and see what happens ...
  137.         */
  138.  
  139.         //-------------------------------TEAM 0-------------------------------------------
  140.  
  141.         Blackboard blackboard1 = new Blackboard(0);
  142.         world.add(blackboard1);
  143.  
  144.         for (int c=0; c < 5; c++) {
  145.             City city1 = new City(0, new Vec2(500.0*Math.random(),1000.0*(Math.random()-0.5)), blackboard1, world);
  146.             world.add(city1);
  147.         }
  148.  
  149.         DefenseTactician dtact = new DefenseTactician(0, new Vec2(600.0,10.0), blackboard1, world);
  150.         world.add(dtact);
  151.  
  152.         //-------------------------------TEAM 1-------------------------------------------
  153.         
  154.         Blackboard blackboard2 = new Blackboard(1);
  155.         world.add(blackboard2);
  156.         
  157.         for (int c=0; c < 5; c++) {
  158.             City city2 = new City(1, new Vec2(-500.0*Math.random(),1000.0*(Math.random()-0.5)), blackboard2, world);
  159.             world.add(city2);
  160.         }
  161.  
  162.         DefenseTactician dtact2 = new DefenseTactician(1, new Vec2(-600.0,-10.0), blackboard2, world);
  163.         world.add(dtact2);
  164.  
  165.         //--------------------------RESOURCES---------------------------
  166.         //Like is many RTS games, there is a resource system. This determines where the 
  167.         //resource pools are and how big they are.
  168.         
  169.         int NUM_RESOURCES = 10;
  170.         
  171.         for (int c=0; c < NUM_RESOURCES; c++) {
  172.             Resource r = new Resource(new Vec2(1000.0*(Math.random()-0.5), 1000.0*(Math.random()-0.5)), 8000.0*Math.random());
  173.             world.add(r);
  174.         }
  175.         
  176.         
  177.         //------------------------------MAIN LOOP-----------------------------
  178.         //It's as simple as...
  179.         
  180.         while(true) {
  181.             theGame.update();
  182.         }
  183.         
  184.     }
  185. }
  186.